package com.kakarote.oa.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kakarote.core.common.annotation.TenantSkip;
import com.kakarote.core.common.enums.SystemCodeEnum;
import com.kakarote.core.exception.CrmException;
import com.kakarote.core.servlet.BaseServiceImpl;
import com.kakarote.core.utils.UserUtil;
import com.kakarote.oa.entity.PO.OaLogFieldConfig;
import com.kakarote.oa.mapper.OaLogFieldConfigMapper;
import com.kakarote.oa.service.IOaLogFieldConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author wanli
 * @description 针对表【wk_oa_log_field_config】的数据库操作Service实现
 * @createDate 2022-07-05 18:08:18
 */
@Service
public class OaLogFieldConfigServiceImpl extends BaseServiceImpl<OaLogFieldConfigMapper, OaLogFieldConfig>
        implements IOaLogFieldConfigService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    @TenantSkip
    public String getNextFieldName(Long configId, Integer fieldType, List<String> existNameList, Integer depth) {
        if (depth < 0) {
            throw new CrmException(SystemCodeEnum.SYSTEM_ERROR);
        }
        Integer esType = parseType(fieldType);
        LambdaQueryWrapper<OaLogFieldConfig> configQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<OaLogFieldConfig> eq = configQueryWrapper
                .select(OaLogFieldConfig::getFieldName)
                .eq(OaLogFieldConfig::getConfigId, configId)
                .eq(OaLogFieldConfig::getFieldType, esType);
        if (existNameList.size() > 0) {
            eq.notIn(OaLogFieldConfig::getFieldName, existNameList);
        }
        List<String> nameList = listObjs(configQueryWrapper, Object::toString);
        if (nameList.size() == 0) {
            String name;
            try {
                name = "field_" + RandomUtil.randomString(RandomUtil.BASE_CHAR, 6);
                OaLogFieldConfig crmFieldConfig = new OaLogFieldConfig();
                crmFieldConfig.setFieldType(esType);
                crmFieldConfig.setConfigId(configId);
                crmFieldConfig.setCreateTime(LocalDateTime.now());
                crmFieldConfig.setCreateUserId(UserUtil.getUserId());
                crmFieldConfig.setFieldName(name);
                save(crmFieldConfig);
            } catch (Exception e) {
                log.error("保存出现冲突", e);
                name = getNextFieldName(configId, fieldType, existNameList, --depth);
            }
            nameList.add(name);
        }
        return nameList.get(0);
    }

    /**
     * 初始化成elastic数据类型
     *
     * @param fieldType 字段类型
     * @return esType 1 keyword 2 date 3 number 4 nested 5 datetime
     */
    private Integer parseType(Integer fieldType) {
        int four = 4;
        Integer[] nested = new Integer[]{2, 9};
        Integer[] date = new Integer[]{4, 13};
        Integer[] number = new Integer[]{5, 6};
        if (Arrays.asList(nested).contains(fieldType)) {
            return 4;
        }
        if (Arrays.asList(date).contains(fieldType)) {
            if (Objects.equals(four, fieldType)) {
                return 2;
            } else {
                return 5;
            }
        }
        if (Arrays.asList(number).contains(fieldType)) {
            return 3;
        }
        return 1;
    }


}




